<?php
class Users extends Controller
{
	private $tbl = 'tbl_admin_login_info';
	
 	function Users()
	{
		parent::Controller();
        $this->load->model('users_model');
	}
	
	function index()
	{
		$data['total_users'] = $this->users_model->total_users();
		create_pagination($data['total_users'], 3, config_item('per_page'));
		$data['users'] = $this->users_model->get_users(config_item('per_page'), $this->uri->segment(3));
		//debug_array($data['users']);exit;
		$data['page_title']	= 'Manage Users';
		$data['page'] = 'users_list_v';
		$this->load->view('main', $data);	
	}
	
	function add()
	{
		if($this->input->post('submit_user'))
		{
			$this->add_edit();	
		}
		
		$data['page_title']	 = 'Add New User';
		$data['page'] = 'user_add_v';
		$this->load->view('main', $data);
	}
	
	function edit($id)
	{
		if($this->input->post('submit_user'))
		{
			$this->add_edit($id);	
		}
		//$data['categories'] = $this->general_db_model->getAll($this->tbl, 'member_name', 'parent_id = 0');
		$data['_user'] = $this->general_db_model->getById($this->tbl, 'id', $id);
		$data['page_title']	 = 'Update User';
		$data['page'] = 'user_add_v';
		$this->load->view('main', $data);	
	}
	
	function add_edit($id = NULL)
	{
		$this->load->model('general_model');
		 $not = array('submit_user', 're_password', 'password');
		 $post = $this->general_model->get_post_array($not);
		 
		 $post['group'] = 'admin';
		 
		 $password = $this->input->post('password');
		 if(!empty($password))
		 	$post['password'] = md5($password);
		 
		 //debug_array($post);
		 //die('ok');
		 if($id)
		 {
		 	$this->general_db_model->update($this->tbl, $post, 'id = '.$id);
			$this->session->set_flashdata('success_message', "Member - <strong>".$post['first_name']." ".$post['last_name']." </strong>Successfully updated.");
		 }
		 else
		 {
		 	$this->general_db_model->insert($this->tbl, $post);
		 	$this->session->set_flashdata('success_message', "User - <strong>".$post['first_name']." ".$post['last_name']." </strong>Successfully added.");
		 }
		 redirect(current_url());
	}
	
	function delete_user($user_id)
	{
		$this->general_db_model->delete($this->tbl, 'id = '.$user_id);
		redirect(filter_url('delete_user'));
	}
}

?>